/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamankodeglsupplier;

import org.sadhar.sia.framework.ClassApplicationModule;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class PerekamanKodeGLSupplierWnd extends ClassApplicationModule {

    private Textbox txtboxKunciPencarian;
    private Textbox txtboxKodeSupplier;
    private Label lblNama;
    private Label lblAlamat;
    private Label lblTelp;
    private Textbox txtboxKodeGLSupplier;
    private Button btnSimpan;
    private Button btnBatal;
    private Listbox lstboxSupplier;
    private Button btnCariSupplier;
    private PerekamanKodeGLSupplierDAO supplierDAO;
    private Listbox lstboxDaftarSupplier;

    public PerekamanKodeGLSupplierWnd() {
        supplierDAO = new PerekamanKodeGLSupplierDAOImpl();
    }

    public void onCreate() throws Exception {
        txtboxKunciPencarian = (Textbox) getFellow("txtboxKunciPencarian");
        txtboxKodeSupplier = (Textbox) getFellow("txtboxKodeSupplier");
        lblNama = (Label) getFellow("lblNama");
        lblAlamat = (Label) getFellow("lblAlamat");
        lblTelp = (Label) getFellow("lblTelp");
        txtboxKodeGLSupplier = (Textbox) getFellow("txtboxKodeGLSupplier");
        btnSimpan = (Button) getFellow("btnSimpan");
        btnBatal = (Button) getFellow("btnBatal");
        btnCariSupplier = (Button) getFellow("btnCariSupplier");
        lstboxDaftarSupplier = (Listbox) getFellow("lstboxDaftarSupplier");

        clear();
        load();
    }

    public void clear() {
        txtboxKunciPencarian.setValue("");
        txtboxKodeSupplier.setValue("");
        txtboxKodeGLSupplier.setValue("");
        lblNama.setValue(".........");
        lblAlamat.setValue(".........");
        lblTelp.setValue(".........");
    }
    private int no = 1;

    public void load() throws Exception {
        int no = 1;
        int i = 0;
        lstboxDaftarSupplier.getItems().clear();
        for (final PerekamanKodeGLSupplier supplier : supplierDAO.list()) {
            Listitem item = new Listitem();
            item.setValue(supplier);
            item.appendChild(new Listcell(String.valueOf(no++)));
            item.appendChild(new Listcell(supplier.getNamaSupplier()));
            item.appendChild(new Listcell(supplier.getAlamat()));
            item.appendChild(new Listcell(supplier.getTelp()));
            item.appendChild(new Listcell(supplier.getKodeGLSupplier()));

            Listcell cellAksi = new Listcell();
            Toolbarbutton btnUbah = new Toolbarbutton();
            btnUbah.setLabel("[ubah]");
            btnUbah.setId("btnUbah" + (i++));

            btnUbah.addEventListener("onClick", new EventListener() {

                public void onEvent(Event arg0) throws Exception {
                    disableComponent(false);
                    lblNama.setValue(supplier.getNamaSupplier());
                    lblTelp.setValue(supplier.getTelp());
                    lblAlamat.setValue(supplier.getAlamat());
                    txtboxKodeGLSupplier.setValue(supplier.getKodeGLSupplier());
                    txtboxKodeSupplier.setValue(supplier.getKodeSupplier() + "");
                    btnCariSupplier.setDisabled(true);
                }
            });
            cellAksi.appendChild(btnUbah);
            item.appendChild(cellAksi);
            lstboxDaftarSupplier.appendChild(item);

        }
    }

    public void disableComponent(boolean value) {
        txtboxKodeGLSupplier.setDisabled(value);
    }

    public void lstboxDaftarSupplierOnSelect() throws Exception {
        disableComponent(true);
        PerekamanKodeGLSupplier supplier = (PerekamanKodeGLSupplier) lstboxDaftarSupplier.getSelectedItem().getValue();
        lblNama.setValue(supplier.getNamaSupplier());
        lblTelp.setValue(supplier.getTelp());
        lblAlamat.setValue(supplier.getAlamat());
        txtboxKodeGLSupplier.setValue(supplier.getKodeGLSupplier());
        txtboxKodeSupplier.setValue(supplier.getKodeSupplier() + "");
    }

    public void btnCariSupplierOnClick() throws Exception {
        if (supplierDAO.list(txtboxKunciPencarian.getValue()).size() == 1) {
            PerekamanKodeGLSupplier supplier = (PerekamanKodeGLSupplier) supplierDAO.list(txtboxKunciPencarian.getValue()).get(0);
            lblNama.setValue(supplier.getNamaSupplier());
            lblTelp.setValue(supplier.getTelp());
            lblAlamat.setValue(supplier.getAlamat());
            txtboxKodeGLSupplier.setValue(supplier.getKodeGLSupplier());
            txtboxKodeSupplier.setValue(supplier.getKodeSupplier() + "");
            btnCariSupplier.setDisabled(true);
        } else {
            final Window win = (Window) Executions.createComponents("/sippzul/perekamankodeglsupplier/SupplierPopup.zul", this, null);
            Textbox txtboxKeySearch = (Textbox) win.getFellow("txtboxKeySearch");
            txtboxKeySearch.setValue(txtboxKunciPencarian.getValue());
            win.doModal();
        }
    }

    public void btnBatalOnClick() throws Exception {
        btnCariSupplier.setDisabled(false);
        clear();
        load();
        disableComponent(false);
    }

    public void btnSimpanOnClick() throws Exception {
        if(txtboxKodeSupplier.getValue().isEmpty()){
            Messagebox.show("Data Supplier belum terpilih","Pesan Informasi",Messagebox.OK,Messagebox.INFORMATION);
            return;
        }
        if (txtboxKodeGLSupplier.getValue().isEmpty()) {
            Messagebox.show("Silahkam masukan Kode GL Supplier", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
            return;
        }
        PerekamanKodeGLSupplier supplier = new PerekamanKodeGLSupplier();
        supplier.setKodeSupplier(Integer.valueOf(txtboxKodeSupplier.getValue()));
        supplier.setKodeGLSupplier(txtboxKodeGLSupplier.getValue());
        try {
            if (supplierDAO.select(supplier.getKodeSupplier(), supplier.getKodeGLSupplier()).size() <= 0) {
                supplierDAO.update(supplier);
            } else {
                Messagebox.show("Kode GL Supplier yang dimasukan sudah ada, silahkan masukan Kode GL yang lain", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
                return;
            }
        } catch (Exception ex) {
            Messagebox.show("Gagal menyimpan data: " + ex.getMessage(), "Pesan Kesalahan", Messagebox.OK, Messagebox.ERROR);
        }
        btnCariSupplier.setDisabled(false);
        clear();
        load();
    }
}
